import Vue from "vue";
import App from "./App";
import http from "@/utils/http";
Vue.config.productionTip = false;
Vue.prototype.$http = http;
import { makePhoneCall } from "./utils/phone";
Vue.prototype.$makePhoneCall = makePhoneCall;
import { isEmpty } from "@/utils/util";

import uView from "uview-ui";
Vue.use(uView);
// // 如此配置即可
uni.$u.config.unit = "rpx";
import EmptyData from "./pages/common/empty-data.vue";

// 全局注册空数据组件
Vue.component("empty-data", EmptyData);
App.mpType = "app";

const app = new Vue({
	...App,
});
app.$mount();


import shareFlexVue from "./components/shareFlex.vue";
Vue.component('shareFlex', shareFlexVue)

Vue.mixin({
	onShow() {
		this.initWeixinConfig();
		// 路由变化监听 - 页面显示
		// this.handleRouteChange('show');
	},
	onLoad(options) {
		// 路由变化监听 - 页面加载
		this.handleRouteChange('load', options);
		if (!isEmpty(options) && options.pid) {
			uni.setStorageSync('pid', options.pid)
			// uni.showModal({
			//   title: '提示',
			//   content: '分享成功，' + options.pid,
			//   showCancel: false,
			//   confirmText: '确定',
			//   success: (res) => {
			//     if(res.confirm){
			//     }
			//   }
			// })
		}
	},
	onHide() {
		// 路由变化监听 - 页面隐藏
		// this.handleRouteChange('hide');
	},
	onUnload() {
		// 路由变化监听 - 页面卸载
		// this.handleRouteChange('unload');
	},
	methods: {
		// 路由变化处理函数
		handleRouteChange(type, options = {}) {

			const currentRoute = this.$mp?.route || '';
			const currentPage = this.$mp?.page || {};

			// console.log(`路由变化监听 - ${type}:`, {
			//   route: currentRoute,
			//   page: currentPage,
			//   options: options,
			//   timestamp: new Date().toLocaleString()
			// });
		},


		// 检测是否是微信环境
		isWeiXin() {
			return /micromessenger/i.test(navigator.userAgent);
		},
		// 初始化微信配置
		initWeixinConfig() {
			if (this.isWeiXin()) {
				this.getWeixinSignature();
			} else {
				uni.showToast({
					title: "配置微信config----当前环境不是微信环境",
					icon: "none",
				});
			}
		},
		// 调用接口获取微信签名
		async getWeixinSignature() {
			try {
				const app = getApp();

				const signUrl =
					app.platform === "ios" ?
					uni.getStorageSync("firstIosUrl") :
					window.location.href.split("#")[0];
				const params = {
					url: signUrl,
				};
				const res = await this.$http.post("/h5/wechat/getSignPackage", params);

				// 添加调试信息
				console.log('微信配置参数:', {
					appId: res.data.appId,
					timestamp: res.data.timestamp,
					signature: res.data.signature
				});

				wx.config({
					debug: false,
					appId: res.data.appId,
					nonceStr: res.data.nonceStr,
					rawString: res.data.rawString,
					timestamp: res.data.timestamp,
					signature: res.data.signature,
					jsApiList: [
						"updateAppMessageShareData",
						"updateTimelineShareData",
						"getLocation",
						"requestMerchantTransfer",
						"saveImageToPhotosAlbum",
						"downloadImage"
					],
				});

				let sharePrefix =
					app.platform === "ios" ?
					uni.getStorageSync("firstIosUrl") :
					window.location.href.split("#")[0];
				wx.ready(function() {
					console.log('微信配置成功，开始设置分享');
					let link = `${sharePrefix}${window.location.hash}`
					if (link.includes('?')) {
						link = `${link}&pid=${uni.getStorageSync('userInfo').id}`
					} else {
						link = `${link}?pid=${uni.getStorageSync('userInfo').id}`
					}
					console.log('分享链接:', link);

					wx.updateAppMessageShareData({
						title: "拼车出行，绿色环保",
						desc: "发现更多拼车路线，省钱又环保",
						link: link,
						imgUrl: "https://api.mszcdf.cn/storage/share.png",
						success: function() {
							console.log('分享到朋友成功');
						},
						fail: function(err) {
							console.error('分享到朋友失败:', err);
							uni.showToast({
								title: "分享失败: " + (err.errMsg || JSON.stringify(
									err)),
								icon: "none",
							});
						},
					});
					wx.updateTimelineShareData({
						title: "拼车出行，绿色环保",
						desc: "发现更多拼车路线，省钱又环保",
						link: link,
						imgUrl: "https://api.mszcdf.cn/storage/share.png",
						success: function() {
							console.log('分享到朋友圈成功');
						},
						fail: function(err) {
							console.error('分享到朋友圈失败:', err);
							uni.showToast({
								title: "分享失败: " + (err.errMsg || JSON.stringify(
									err)),
								icon: "none",
							});
						},
					});

				});
			} catch (error) {}
		},
	},
});